﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YJKArch.MapNameTag
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class MapNameTag : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Autodesk.Revit.DB.Document document = activeUIDocument.Document;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			if (MapNameTag._hWndRevit == null)
			{
				Process[] processesByName = Process.GetProcessesByName("Revit");
				if (processesByName.Length != 0)
				{
					MapNameTag._hWndRevit = new WindowHandle(processesByName[0].MainWindowHandle);
				}
			}
			IListDate.FontFamily = Helper.GetSystemFamilyElementList<TextNoteType>(document);
			MapNameTagForm mapNameTagForm = new MapNameTagForm();
			mapNameTagForm.Show(MapNameTag._hWndRevit);
			int wMsg = 7;
			MapNameTag.SendMessage(MapNameTag._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Autodesk.Revit.Creation.Application create = application.Application.Create;
			for (;;)
			{
				Transaction transaction = new Transaction(document);
				transaction.Start("revit");
				XYZ xyz = null;
				try
				{
					xyz = activeUIDocument.Selection.PickPoint("请选择一个点");
				}
				catch (Exception)
				{
					transaction.RollBack();
					mapNameTagForm.Close();
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				if (mapNameTagForm.MapNameStr == null || mapNameTagForm.MapNameStr.Length == 0)
				{
					TaskDialog.Show("图名不能为空", "图名不能为空");
					transaction.RollBack();
				}
				else
				{
					XYZ origin = null;
					double num = 0.0;
					Element selectType = Helper.GetSelectType<TextNoteType>(document, mapNameTagForm.FontType);
					Element selectType2 = Helper.GetSelectType<TextNoteType>(document, mapNameTagForm.proportionFontType);
					double num2 = selectType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0 * (double)activeView.Scale / 100.0;
					double num3 = selectType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
					byte[] bytes = Encoding.Default.GetBytes(mapNameTagForm.MapNameStr);
					double num4 = num2 * num3;
					double num5 = (double)bytes.Length * num4 / 1.7;
					double num6 = selectType2.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0 * (double)activeView.Scale / 100.0;
					double num7 = selectType2.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
					num = num6 * num7;
					double num8 = 0.0;
					XYZ origin2;
					if (num2 > num6)
					{
						num8 = num2;
						origin2 = new XYZ(xyz.X, xyz.Y, xyz.Z);
						origin = new XYZ(xyz.X + num5 + 0.5, xyz.Y - (num2 - num6), xyz.Z);
					}
					else
					{
						num8 = num6;
						origin2 = new XYZ(xyz.X, xyz.Y - (num6 - num2), xyz.Z);
						origin = new XYZ(xyz.X + num5 + 0.5, xyz.Y, xyz.Z);
					}
					if (xyz == null)
					{
						break;
					}
					XYZ baseVec = new XYZ(1.0, 0.0, 0.0);
					XYZ upVec = new XYZ(0.0, 0.0, 1.0);
					string mapNameStr = mapNameTagForm.MapNameStr;
                    TextNote textNode = document.GetTextNode(activeView, origin2, baseVec, upVec, 0.001, (TextAlignFlags)64, mapNameStr);
					try
					{
						textNode.TextNoteType = (selectType as TextNoteType);
					}
					catch (Exception)
					{
						transaction.RollBack();
					}
					textNode.Width = num5 * 1.7;
					string proportionSelect = mapNameTagForm.proportionSelect;
					if (mapNameTagForm.isChecked)
					{
                        TextNote textNode2 = document.GetTextNode(activeView, origin, baseVec, upVec, 1.0, (TextAlignFlags)64, proportionSelect);
						textNode2.TextNoteType = (selectType2 as TextNoteType);
						double num9 = num * (double)proportionSelect.Length / 1.7;
						textNode2.Width = num9 * 1.7;
						if (mapNameTagForm.traditionOrNational && mapNameTagForm.isChecked)
						{
							num5 = num5 + num9 + 0.5;
						}
					}
					XYZ startPoint = new XYZ(xyz.X, xyz.Y - num8 / 1.7 * 2.0 - 0.2 * (double)activeView.Scale / 100.0, xyz.Z);
					XYZ endPoint = new XYZ(xyz.X + num5, xyz.Y - num8 / 1.7 * 2.0 - 0.2 * (double)activeView.Scale / 100.0, xyz.Z);
					XYZ startPoint2 = new XYZ(xyz.X, xyz.Y - num8 / 1.7 * 2.0 - 0.4 * (double)activeView.Scale / 100.0, xyz.Z);
					XYZ endPoint2 = new XYZ(xyz.X + num5, xyz.Y - num8 / 1.7 * 2.0 - 0.4 * (double)activeView.Scale / 100.0, xyz.Z);
					Curve curve = YJKLineEx.YJKGetBound(startPoint, endPoint);
					document.Create.NewDetailCurve(activeView, curve);
					if (mapNameTagForm.traditionOrNational)
					{
						Curve curve2 = YJKLineEx.YJKGetBound(startPoint2, endPoint2);
						document.Create.NewDetailCurve(activeView, curve2);
					}
					transaction.Commit();
				}
			}
			return Autodesk.Revit.UI.Result.Failed;
		}

		public static WindowHandle _hWndRevit;
	}
}
